package com.imu.purse.admin.controller.business;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.imu.common.annotation.SysLog;
import com.imu.common.utils.R;
import com.imu.common.validator.ValidatorUtils;
import com.imu.common.validator.group.AddGroup;
import com.imu.common.validator.group.UpdateGroup;
import com.imu.purse.modules.app.entity.SdZblogEntity;
import com.imu.purse.modules.app.service.SdZblogService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2018-07-05 19:22:35
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
@RestController
@RequestMapping("sdzblog")
public class SdZblogController extends com.imu.purse.modules.sys.controller.AbstractController
{
	/****
	 * 查询
	 * @param params
 	 * @date 2018-07-05 19:22:35
	 * @return
	 */
	@SysLog("查询")
	@GetMapping("/list")
	@RequiresPermissions("sdzblog:list")
	public R list(@RequestParam Map<String, Object> params)
	{
		R r = R.ok();
		if(CollectionUtils.isEmpty(params)) return r;
		
		SdZblogEntity dto = com.xiaoleilu.hutool.bean.BeanUtil.mapToBean(params, SdZblogEntity.class, true);
		if(StringUtils.isEmpty(dto.getPayPhone()) && StringUtils.isEmpty(dto.getRecPhone()) && dto.getStime() == null
				&& dto.getEtime() == null && dto.getTid() == null && StringUtils.isEmpty(dto.getBid())){
			return r.put("page", new PageInfo<SdZblogEntity>(Lists.newArrayList()));
		}
		if(dto.getStime() != null){
			dto.setStime(dto.getStime()/1000);
		}
		if(dto.getEtime() != null){
			dto.setEtime(dto.getEtime()/1000);
		}
		List<SdZblogEntity> dtos = service.queryList(dto);
		if(CollectionUtils.isEmpty(dtos)) return r;
		
		return r.put("page", new PageInfo<SdZblogEntity>(dtos));
	}
	
	/****
	 * 根据ID查询
	 * @param id
 	 * @date 2018-07-05 19:22:35
	 * @return
	 */
	@SysLog("根据ID查询")
	@GetMapping("/info/{id}")
	@RequiresPermissions("sdzblog:info")
	public R info(@PathVariable("id") Integer id)
	{
		return R.ok().put("dto", service.findById(id));
	}
	
	/****
	 * 新增
	 * @param dto
 	 * @date 2018-07-05 19:22:35
	 * @return
	 */
	@SysLog("新增")
	@PostMapping("/save")
	@RequiresPermissions("sdzblog:save")
	public R save(@RequestBody SdZblogEntity dto)
	{
		ValidatorUtils.validateEntity(dto, AddGroup.class);
		service.save(dto);
		return R.ok();
	}
	
	/****
	 * 修改
	 * @param dto
 	 * @date 2018-07-05 19:22:35
	 * @return
	 */
	@SysLog("修改")
	@PostMapping("/update")
	@RequiresPermissions("sdzblog:update")
	public R update(@RequestBody SdZblogEntity dto)
	{
		ValidatorUtils.validateEntity(dto, UpdateGroup.class);
		service.update(dto);
		return R.ok();
	}
	
	/****
	 * 删除
	 * @param ids
 	 * @date 2018-07-05 19:22:35
	 * @return
	 */
	@SysLog("删除")
	@PutMapping("/delete")
	@RequiresPermissions("sdzblog:delete")
	public R delete(@RequestBody String[] ids)
	{
		service.delete(ids);
		return R.ok();
	}
	
	@Autowired
	private SdZblogService service;

	public static void main(String[] args){
		Calendar cal = Calendar.getInstance();
		//构建开始时间
		cal.set(2018, 0,1,0,0,0);
		System.out.println(cal.getTimeInMillis()/1000);
		//构建结束时间
		cal.set(2018, 11,31,23,59,59);
		System.out.println(cal.getTimeInMillis()/1000);
	}
}
